package com.enjoyor.soa.traffic.core.tsms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.tsms.domain.AssessItemsScore;
import com.enjoyor.soa.traffic.core.tsms.domain.PoliceAssess;
import com.enjoyor.soa.traffic.core.tsms.mapper.IAssessItemsScoreMapper;
import com.enjoyor.soa.traffic.core.tsms.mapper.IPoliceAssessMapper;
import com.enjoyor.soa.traffic.core.tsms.service.IPoliceAssessService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;

@Service
@Transactional
@SuppressWarnings("rawtypes")
public class PoliceAssessService implements IPoliceAssessService{

	@Autowired
	private IPoliceAssessMapper iPoliceAssessMapper;
	
	@Autowired
	private IAssessItemsScoreMapper iAssessItemsScoreMapper;
	
	@Autowired
	private IPubLogMapper iPubLogMapper;
	
	@Override
	public Page getPoliceAssessPage(Page page, String assessId, String executeTaskId, String policeId) {
		// TODO Auto-generated method stub
		Map<String, Object> mapBean = new HashMap<>();
		mapBean.put("assessId", assessId);
		mapBean.put("executeTaskId", executeTaskId);
		mapBean.put("policeId", policeId);
		page.setMapBean(mapBean);
		return iPoliceAssessMapper.getPoliceAssess(page);
	}

	@Override
	public List<PoliceAssess> getPoliceAssessList(String assessId, String executeTaskId, String policeId) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<>();
		map.put("assessId", assessId);
		map.put("executeTaskId", executeTaskId);
		map.put("policeId", policeId);
		return iPoliceAssessMapper.getPoliceAssessList(map);
	}

	@Override
	public void addPoliceAssess(PoliceAssess policeAssess, String personName) {
		// TODO Auto-generated method stub
		policeAssess.setAssessId("PA"+TimeHelper.getUniqueString());
		policeAssess.setAssessTime(TimeHelper.dateToLongString());
		policeAssess.setAssessPerson(personName);
		iPoliceAssessMapper.insertSelective(policeAssess);
		iPubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
                String.format("添加考核:{%s} 操作成功!", new Object[] { policeAssess.getId() }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_POLICE_ASSESS_MANAGEMENT.getMessage());
	}

	@Override
	public void editPoliceAssess(PoliceAssess policeAssess, String personName) {
		// TODO Auto-generated method stub
		iPoliceAssessMapper.updateById(policeAssess);
		iPubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
                String.format("修改考核:{%s} 操作成功!", new Object[] { policeAssess.getId() }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_POLICE_ASSESS_MANAGEMENT.getMessage());
	}

	@Override
	public void deletePoliceAssess(String assessId, String executeTaskId, String policeId, String personName) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<>();
		map.put("assessId", assessId);
		map.put("executeTaskId", executeTaskId);
		map.put("policeId", policeId);
		iPoliceAssessMapper.deletePoliceAssess(map);
		iPubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
                String.format("删除考核:{%s} 操作成功!", new Object[] { policeId }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_POLICE_ASSESS_MANAGEMENT.getMessage());
	}

	@Override
	public void assessPoliceScore(PoliceAssess policeAssess, List<AssessItemsScore> list, String personName) {
		// TODO Auto-generated method stub
		if(policeAssess.getAssessId()==null){
			String assessId = "PA"+TimeHelper.getUniqueString();
			policeAssess.setAssessId(assessId);
			policeAssess.setAssessTime(TimeHelper.dateToLongString());
			policeAssess.setAssessPerson(personName);
			iPoliceAssessMapper.insertSelective(policeAssess);
			for(int i = 0;i<list.size();i++){
				list.get(i).setAssessId(assessId);
				list.get(i).setScoreId("AS"+TimeHelper.getUniqueString());
				iAssessItemsScoreMapper.insertSelective(list.get(i));
			}
		}else{
			policeAssess.setAssessTime(TimeHelper.dateToLongString());
			policeAssess.setAssessPerson(personName);
			iPoliceAssessMapper.updateById(policeAssess);
			iAssessItemsScoreMapper.deleteById(policeAssess.getAssessId());
			for(int i = 0;i<list.size();i++){
				list.get(i).setAssessId(policeAssess.getAssessId());
				list.get(i).setScoreId("AS"+TimeHelper.getUniqueString());
				iAssessItemsScoreMapper.insertSelective(list.get(i));
			}
		}
		iPubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
                String.format("考核:{%s} 操作成功!", new Object[] {policeAssess.getId()}),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_POLICE_ASSESS_MANAGEMENT.getMessage());
	}
}
